/*
https://leetcode-cn.com/problems/super-washing-machines/solution/chao-ji-xi-yi-ji-by-leetcode-solution-yhej/
 */
public class Solution517 {
    public int findMinMoves(int[] machines) {
        int total=0;
        for (int machine : machines) {
            total=total+machine;
        }
        if (total%machines.length!=0){
            return -1;
        }
        int avg=total/machines.length;
        int ans=0;
        int sum=0;
        for (int machine : machines) {
            machine=machine-avg;
            sum=sum+machine;
            ans=Math.max(ans,machine);
            ans=Math.max(ans,Math.abs(sum));
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution517().findMinMoves(new int[]{1,0,5}));
    }
}
